﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;

namespace MyAO
{
    class AOUtil
    {
        # region 静态成员

        static private IdentifyForm m_identifyForm = null;//查询窗体
        static private double SEARCHTOLPIXELS = 4;//容差像素值

        # endregion

        # region Identify
        /// <summary>
        /// 显示Identify窗口
        /// </summary>
        /// <param name="axMapControl">查询对象所在的图层</param>
        static public void Identify(AxMapControl axMapControl, int x, int y)
        {
            // 先点选要素
            SelectByPoint(axMapControl, x, y);

            if (m_identifyForm == null)            
                m_identifyForm = new IdentifyForm(axMapControl);

            m_identifyForm.Show();
            m_identifyForm.BringToFront();

            //清空下拉列表内容
            m_identifyForm.RefillContent();

         }

        /// <summary>
        /// 将m_IdentifyForm置为空
        /// </summary>
        static public void ClearIdentifyForm()
        {
            m_identifyForm = null;
        }
            # region 点选要素函数

            /// <summary>
            /// 点选要素-以一定容差
            /// </summary>
            public static void SelectByPoint(AxMapControl axMapControl, int x, int y)
            {
                IActiveView pActiveView = axMapControl.ActiveView;
                IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
                IEnvelope pEnvelope = pPoint.Envelope;

                double tolDistance = ConvertPixelsToMapUnits(pActiveView, SEARCHTOLPIXELS);
                pEnvelope.Width = tolDistance;
                pEnvelope.Height = tolDistance;
                pEnvelope.CenterAt(pPoint);

                IGeometry pGeometry = pEnvelope as IGeometry;
                pGeometry.SpatialReference = axMapControl.Map.SpatialReference;
                axMapControl.Map.SelectByShape(pGeometry, null, false);
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, pActiveView.Extent);
            }

            /// <summary>
            /// 根据像素容差计算地图坐标单位容差值
            /// </summary>
            /// <param name="pActiveView">当前活动视图</param>
            /// <param name="pixelUnits">容差像素</param>
            /// <returns>地图坐标单位容差值</returns>
            public static double ConvertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits)
            {
                ESRI.ArcGIS.esriSystem.tagRECT deviceRect = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame();
                int pixelExtent = deviceRect.right - deviceRect.left;
                double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
                double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
                return pixelUnits * sizeOfOnePixel;
            }

            # endregion

        # endregion

        # region 获得几何要素IGeometry
        /// <summary>
        /// 返回被选择的第一个几何元素
        /// </summary>
        public static IGeometry GetSelectGeometry(AxMapControl axMapControl)
        {

            for (int i = 0; i < axMapControl.LayerCount; i++)
            {
                IFeatureLayer pFeatureLayer = axMapControl.get_Layer(i) as IFeatureLayer;
                IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
                ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet;

                ICursor pCursor = null;
                pSelectionSet.Search(null, false, out pCursor);
                IFeatureCursor pFeatureCursor = pCursor as IFeatureCursor;
                IFeature pFeature = pFeatureCursor.NextFeature();

                System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
                GC.Collect();
                GC.WaitForPendingFinalizers();

                if (pFeature != null) return pFeature.Shape;
            }

            return null;
        }

        /// <summary>
        /// 获得外包型缓冲区
        /// </summary>
        /// <param name="pGeometry">要建立缓冲区的几何要素</param>
        /// <param name="bufferDis">缓冲区半径(km)</param>
        /// <returns>外包型缓冲区要素</returns>
        public static IGeometry CreateOutFillBuffer(IGeometry pGeometry, double bufferDis)
        {
            if (pGeometry == null || bufferDis<=0) return null;

            ITopologicalOperator pTopologicalOperator = pGeometry as ITopologicalOperator;
            IGeometry pBufferGeometry = pTopologicalOperator.Buffer(bufferDis);

            return pBufferGeometry;
        }

        # endregion

        # region 获得图层ILayer

        /// <summary>
        /// 从名字获取层
        /// </summary>
        /// <param name="name">图层名字</param>
        /// <returns>ILayer接口</returns>
        public static ILayer GetLayerByName(AxMapControl axMapControl, String name)
        {
            for (int i = 0; i < axMapControl.LayerCount; i++)
            {
                ILayer pLayer = axMapControl.get_Layer(i);
                if (pLayer.Name == name) return pLayer;
            }

            return null;
        }

        # endregion
    }

    public enum GISTool
    {
        Default = 0,
        Pan = 1,
        ZoomIn = 2,
        ZoomOut = 3,
        Select = 4,
        Identify = 5,
    }
        
}
